#include "client.h"



int check_cmd(char * token)
{
    int i=0;
    char buf[32];
    bzero(buf,sizeof(buf));
    for(;token[i]!='\0';i++)
    {
        buf[i]=toupper(token[i]);
    }
    /* if(strcmp(buf,"DATA")==0) return DATA; */
    if(strcmp(buf,"CD")==0) return CD;
    if(strcmp(buf,"LS")==0) return LS;
    if(strcmp(buf,"PUTS")==0) return PUTS;
    if(strcmp(buf,"GETS")==0) return GETS;
    if(strcmp(buf,"REMOVE")==0) return REMOVE;
    if(strcmp(buf,"PWD")==0) return PWD;
    if(strcmp(buf,"MKDIR")==0) return MKDIR;

    /* printf("无效命令!\n"); */
    return DATA;

}










int send_cmd(char *line,int sockfd)
{
    train_t train;
    bzero(&train,sizeof(train));
    char *token;
    token = strtok(line," ");
   int ret= check_cmd(token);
   train.type=ret;
   send(sockfd,&train.type,sizeof(train.type),0);
   char buf[16];
   switch(train.type)
   {
   case CD:
       while(1){
           bzero(buf,sizeof(buf));
           bzero(&train,sizeof(train));
           token = strtok(NULL,"/\n");
           if(NULL==token)
           {
               break;
           }
           strcpy(buf,token);
           strcat(buf,"/");
           ret = check_cmd(token);
           train.type=ret;
           if(train.type==DATA)
           {
               send(sockfd,&train.type,sizeof(train.type),0);
               train.len=strlen(buf);
               send(sockfd,&train.len,sizeof(train.len),0);
               strcpy(train.data,buf);
               send(sockfd,train.data,train.len,0);
           }
       }
       train.type=DATA;
       send(sockfd,&train.type,sizeof(train.type),0);
       train.len=0;
       send(sockfd,&train.len,sizeof(train.len),0);
       break;
   case LS: 
       train.type=DATA;
       send(sockfd,&train.type,sizeof(train.type),0);
       train.len=0;
       send(sockfd,&train.len,sizeof(train.len),0);      
       while(1)
       {
           bzero(&train,sizeof(train));
           ssize_t sret = recv(sockfd,&train.type,sizeof(train.type),MSG_WAITALL);
           ERROR_CHECK(sret,-1,"recv");
           if(sret==0)
           {
               break;
           }
           /* if(train.type==LS) */
           /* { */
               sret = recv(sockfd,&train.len,sizeof(train.len),MSG_WAITALL);
               ERROR_CHECK(sret,-1,"recv");
               sret = recv(sockfd,train.data,train.len,MSG_WAITALL);
               ERROR_CHECK(sret,-1,"recv");
           /* } */
           printf("%s",train.data);
       }
       printf("\n");
       break;
   case PWD:
       train.type=DATA;
       send(sockfd,&train.type,sizeof(train.type),0);
       train.len=0;
       send(sockfd,&train.len,sizeof(train.len),0);

       while(1)
       {
           bzero(&train,sizeof(train));
           ssize_t sret = recv(sockfd,&train.type,sizeof(train.type),MSG_WAITALL);
           ERROR_CHECK(sret,-1,"recv");
           if(sret==0)
           {
               break;
           }
           /* if(train.type==PWD) */
           /* { */
               sret = recv(sockfd,&train.len,sizeof(train.len),MSG_WAITALL);
               ERROR_CHECK(sret,-1,"recv");
               sret = recv(sockfd,train.data,train.len,MSG_WAITALL);
               ERROR_CHECK(sret,-1,"recv");
           /* } */
           printf("%s",train.data);
       }
       printf("\n");
       break;
   case MKDIR:
        char buf1[16];
       while(1){
           bzero(buf1,sizeof(buf1));
           bzero(&train,sizeof(train));
           token = strtok(NULL,"/\n");
           if(NULL==token)
           {
               break;
           }
           strcpy(buf1,token);
           strcat(buf1,"/");
           ret = check_cmd(token);
           train.type=ret;
           send(sockfd,&train.type,sizeof(train.type),0);
           train.len=strlen(buf1);
           send(sockfd,&train.len,sizeof(train.len),0);
           strcpy(train.data,buf1);
           send(sockfd,train.data,train.len,0);
       }
       train.type=DATA;
       send(sockfd,&train.type,sizeof(train.type),0);
       train.len=0;
       send(sockfd,&train.len,sizeof(train.len),0);
       break;
   case PUTS:
       char filename[20];
       bzero(filename,sizeof(filename));
       token = strtok(NULL," \n");
       strcpy(filename,token);
       ret = check_cmd(token);
       train.type=ret;
       send(sockfd,&train.type,sizeof(train.type),0);
       train.len = strlen(token);
       send(sockfd,&train.len,sizeof(train.len),0);
       strcpy(train.data,token);
       send(sockfd,train.data,train.len,0);
       train.type=DATA;
       send(sockfd,&train.type,sizeof(train.type),0);
       train.len=0;
       send(sockfd,&train.len,sizeof(train.len),0);


       //to do 传输文件；
    case GETS:
       bzero(filename,sizeof(filename));
       token = strtok(NULL," \n");
       strcpy(filename,token);
       ret = check_cmd(token);
       train.type=ret;
       send(sockfd,&train.type,sizeof(train.type),0);
       train.len = strlen(token);
       send(sockfd,&train.len,sizeof(train.len),0);
       strcpy(train.data,token);
       send(sockfd,train.data,train.len,0);
       train.type=DATA;
       send(sockfd,&train.type,sizeof(train.type),0);
       train.len=0;
       send(sockfd,&train.len,sizeof(train.len),0);
       //to do 接收数据
    case REMOVE:
       while(1){
           bzero(buf,sizeof(buf));
           bzero(&train,sizeof(train));
           token = strtok(NULL,"/\n");
           if(NULL==token)
           {
               break;
           }
           strcpy(buf,token);
           strcat(buf,"/");
           ret = check_cmd(token);
           train.type=ret;
           if(train.type==DATA)
           {
               send(sockfd,&train.type,sizeof(train.type),0);
               train.len=strlen(buf);
               send(sockfd,&train.len,sizeof(train.len),0);
               strcpy(train.data,buf);
               send(sockfd,train.data,train.len,0);
           }
       }
       train.type=DATA;
       send(sockfd,&train.type,sizeof(train.type),0);
       train.len=0;
       send(sockfd,&train.len,sizeof(train.len),0);
       break;
    default:
       printf("无效命令！\n");
       break;
 

    
   }

   return 0;


}

